const path = require('path');
const events = require('events');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');

events.EventEmitter.defaultMaxListeners = 0; // Possible EventEmitter memory leak detected 关闭内存泄漏警告

function resolve(dir) {
  return path.join(__dirname, dir);
}

const isDevEnv = process.env.NODE_ENV === 'development';

// const mockServer = () => (isDevEnv ? require('./mock/mock-server.js') : '');

// const target = 'https://dev.shanglike.com';
const target = 'https://test.shanglike.com';

module.exports = {
  publicPath: './',
  runtimeCompiler: true,
  lintOnSave: isDevEnv, // 开发环境，保存时进行eslint检查
  // lintOnSave: false, // 开发环境，保存时进行eslint检查
  devServer: {
    hot: true,
    open: true,
    noInfo: false,
    overlay: {
      warnings: true,
      errors: true,
    },
    // after: mockServer(),
    proxy: {
      '/bnd-uaac': {
        target,
        changeOrigin: true,
      },
      '/bnd-admin': {
        target,
        changeOrigin: true,
      },
      '/bnd-goods': {
        target,
        changeOrigin: true,
      },

      '/bonade-order-service': {
        target,
        changeOrigin: true,
      },
      '/shanglike-datacenter': {
        target,
        changeOrigin: true,
      },
      '/bnd-benefits': {
        target,
        changeOrigin: true,
      },
      '/bnd-organization': {
        target,
        changeOrigin: true,
      },
      '/market-service': {
        target,
        changeOrigin: true,
      },
      '/bnd-video': {
        target,
        changeOrigin: true,
      },
      '/uploadOSS': {
        target: 'http://bnd-video-center.oss-cn-shenzhen.aliyuncs.com', //开发环境平台端
        changeOrigin: true,
      },
      '/bonade-order-service': {
        target,
        changeOrigin: true,
      },
    },
  },
  chainWebpack: config => {
    config
      // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development', config => config.devtool('cheap-source-map'));

    if (process.env.NODE_ENV !== 'development') {
      // config.plugin('analyzer').use(BundleAnalyzerPlugin);

      // gzip需要nginx进行配合
      config
        .plugin('compression')
        .use(CompressionWebpackPlugin)
        .tap(() => [
          {
            test: /\.js$|\.html$|\.css/, // 匹配文件名
            threshold: 10240, // 超过10k进行压缩
            deleteOriginalAssets: false, // 是否删除源文件
          },
        ]);
    }
  },
  configureWebpack: {
    entry: {
      app: './src/main.js',
    },
    resolve: {
      alias: {
        '@': resolve('./src'),
      },
      extensions: ['.js', '.vue', '.json', '.css', '.node'],
    },
    optimization: {
      // 模块抽离
      splitChunks: {
        chunks: 'all',
        minSize: 20000,
        minChunks: 1,
        cacheGroups: {
          libs: {
            test: /[\\/]node_modules[\\/]/,
            priority: -10,
          },
          commons: {
            test: resolve('./src/components'), // can customize your rules
            priority: -15,
          },
          async: {
            chunks: 'async',
            maxAsyncRequests: 10,
            priority: -20,
          },
          default: {
            priority: -25,
            reuseExistingChunk: true,
            minChunks: 2,
          },
        },
      },
    },
  },
};
